<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="构件编号" prop="compomentNo">
        <el-input
          v-model="queryParams.compomentNo"
          placeholder="请输入构件编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     
      <el-form-item label="项目名称" prop="projectNo">
        
        <el-select v-model="queryParams.projectNo" filterable allow-create default-first-option placeholder="项目名称">
          <el-option v-for="item in proName" :key="item.projectNo" :label="item.projectName" :value="item.projectNo"
            @click.native="clickOption(proName[index])">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildNo">
        <!-- <el-select v-model="queryParams.buildNo" filterable allow-create default-first-option placeholder="项目名称">
          <el-option v-for="item in buildName" :key="item.value" :label="item.buildNo" :value="item.buildNo">
          </el-option>
        </el-select> -->
        <el-input v-model="queryParams.buildNo" placeholder="请输入楼栋编号" clearable @keyup.enter.native="handleQuery" />

      </el-form-item>
      <el-form-item label="构件" prop="compomentName">
        <el-input v-model="queryParams.compomentName" placeholder="请输入项目编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="楼栋编号" prop="buildNo">
        <el-input
          v-model="queryParams.buildNo"
          placeholder="请输入楼栋编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="楼层" prop="floor">
        <el-input-number v-model="queryParams.beginFloor" controls-position="right" @change="handleChange" :min="0"></el-input-number>至
        <el-input-number v-model="queryParams.endFloor" controls-position="right" @change="handleChange" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="入库时间" prop="warehousedTime">
        <el-date-picker clearable v-model="queryParams.beginWarehousedTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
        - <el-date-picker clearable v-model="queryParams.endWarehousedTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['yard:compWarehouseInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div style="float: right; ">
            构件总数量：<span style="color:red">
                {{count}}
            </span>
            构件总重量：
            <span style="color:red">
                {{weight}}
            </span>
        </div>
    <el-table v-loading="loading" :data="compWarehouseInfoList" @selection-change="handleSelectionChange"
      @cell-click="goclick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="构件编号" align="center" prop="compomentNo" />·
      <el-table-column label="构件名称" align="center" prop="compomentName" />
      <el-table-column label="材质" align="center" prop="texture" />
      <el-table-column label="长度(mm)" align="center" prop="length" />
      <el-table-column label="库存数量" align="center" prop="stockCount" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="楼栋" align="center" prop="buildNo" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="仓库区域" align="center" prop="warehouseArea" />
      <el-table-column label="单净重量(kg)" align="center" prop="netWeight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click.native="handleUpdate(scope.row)"
            v-hasPermi="['yard:compWarehouseInfo:edit']">库位变更</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改构件库存信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form :model="queryParams" :rules="rules" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        line-width="80px">
        <el-form-item label="仓 库:" >
          <span>{{spanWarehouseName}}</span>
        </el-form-item>
        <el-form-item label="-" >
          <span>{{spanWarehouseArea}}</span>
        </el-form-item><br>
        <el-form-item label="变更为:" prop="warehouseArea">
          <el-input v-model="updateWarehouseArea" placeholder="请输入仓库名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="-" prop="warehouseName">
          <el-input v-model="updateWarehouseName" placeholder="请输入仓库区域" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-top:20px;">
        <el-button @click="Offs">取 消</el-button>

        <el-button plain @click="lead">确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
</template>

<script>
import { listCompWarehouseInfo, getCompWarehouseInfo, delCompWarehouseInfo, addCompWarehouseInfo,
   updateCompWarehouseInfo, getProjectInfoList ,getCompWarehouseStatistics} from "@/api/yard/compWarehouseInfo";

export default {
  name: "CompWarehouseInfo",
  data() {
    return {
      count:0,
      weight:0,
      updateWarehouseId:null,
      spanWarehouseName:'',
      spanWarehouseArea:'',
      updateWarehouseName:"",
      updateWarehouseArea:"",
      num: null,
      num1: null,
      // 项目名称下拉框数组=====
      proName: [],
      // 楼栋下拉框数组=========
      buildName: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 构件库存信息表格数据
      compWarehouseInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // 项目名称==========
        projectName: null,
        // 楼栋编号
        buildNo: null,
        compomentName: null,
        stockCount: null,
        texture: null,
        pageNum: 1,
        pageSize: 10,
        compomentNo: null,
        endFloor: 0,
        projectNo: null,
        buildNo: null,
        floor: null,
        warehousedBy: null,
        warehousedTime: null,
        warehouseName: null,
        warehouseArea: null,
        comment: null,
        length: null,
        beginFloor: 0,
        netWeight: null,
        beginWarehousedTime: null,
        endWarehousedTime: null
        
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseName: [
          { required: true, message: "仓库名不能为空", trigger: "change" }
        ],
        warehouseArea: [
          { required: true, message: "仓库区域不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    // listCompWarehouseInfo(this.queryParams).then(res=>{
    //   for(let i = 0;i<res.rows.length;i++){
    //     this.proName = res.rows[i].projectNo
    //     this.buildName = res.rows[i].buildNo
    //     this.buildName = res.rows
    //     this.proName = res.rows
    //   }
    // })
    getProjectInfoList().then(res => {
      console.log(res)
      for (let i = 0; i < res.rows.length; i++) {

        let p = { "projectName": res.rows[i].projectName, "projectNo": res.rows[i].projectNo }
        this.proName.push(p)
      }
    })
    this.getList();
  },
  methods: {
    clickOption(a) {
      console.log("dianji0", a)
    },

    // 弹框导入按钮
    lead() {
     
      
      
      updateCompWarehouseInfo({"id":this.updateWarehouseId,
      "warehouseName":this.updateWarehouseArea,
      "warehouseArea":this.updateWarehouseName}).then(res=>{
        console.log(res);
      })
      this.getList();
      if (this.$refs["queryForm"].validate()) {
        this.open = false
      } else {
        this.open = open
      }

    },
    // 弹框取消按钮

    Offs() {
      this.open = false
    },
    /** 查询构件库存信息列表 */
    getList() {
        
      this.loading = true;
      if(this.queryParams.beginFloor==0 && this.queryParams.endFloor == 0){
        this.queryParams.beginFloor=null;
        this.queryParams.endFloor =null;
        listCompWarehouseInfo(this.queryParams).then(response => {
          this.compWarehouseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        getCompWarehouseStatistics(this.queryParams).then(response => {
          this.count = response.count;
          this.weight = response.weight;
          
        });
        return false;
      }else if(this.queryParams.beginFloor>=this.queryParams.endFloor){
        this.loading = false;
        this.$message.error("开始楼层不能大于结束楼层！！");
        
        return false;
      }else{
        listCompWarehouseInfo(this.queryParams).then(response => {
          this.compWarehouseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        getCompWarehouseStatistics(this.queryParams).then(response => {
          this.count = response.count;
          this.weight = response.weight;
        });
        return false;
      }
      


      
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        compomentNo: null,
        projectNo: null,
        buildNo: null,
        floor: null,
        stockCount: null,
        warehousedBy: null,
        warehousedTime: null,
        warehouseName: null,
        warehouseArea: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        comment: null,
        compomentName: null,
        texture: null,
        length: null,
        projectName: null,
        netWeight: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加构件库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.updateWarehouseId=row.id;
      this.spanWarehouseName = row.warehouseName;
      this.spanWarehouseArea = row.warehouseArea;
      this.reset();
      const id = row.id || this.ids
      getCompWarehouseInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "库位变更";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCompWarehouseInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompWarehouseInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除构件库存信息编号为"' + ids + '"的数据项？').then(function () {
        return delCompWarehouseInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yard/compWarehouseInfo/export', {
        ...this.queryParams
      }, `compWarehouseInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
